package com.tanhua.server.controller;

import com.tanhua.model.dto.CommentDto;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    /**
     * 发布视频
     * 接口路径：POST
     * 请求参数：
     * videoThumbnail：封面图
     * videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/{videoId}/like")
    public ResponseEntity likeVideo(@PathVariable("videoId") String videoId) {
        videosService.likeVideo(videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞-取消
     */
    @PostMapping("/{videoId}/dislike")
    public ResponseEntity dislikeVideo(@PathVariable("videoId") String videoId) {
        videosService.dislikeVideo(videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论列表
     */
    @GetMapping("/{videoId}/comments")
    public ResponseEntity getCommentsOfVideo(@PathVariable("videoId") String videoId,
                                             @RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageInfo = videosService.getCommentsOfVideoByPage(videoId, page, pagesize);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 评论发布
     */
    @PostMapping("/{videoId}/comments")
    public ResponseEntity publishComment(@PathVariable("videoId") String videoId,
                                         @RequestBody CommentDto dto) {
        videosService.publishComment(videoId, dto.getComment());
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long uid) {
        videosService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注-取消
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long uid) {
        videosService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }
}